package Unidad1.Eventos;

import java.awt.Color;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import Utilerias.Apariencia;

/**
 * @version 2
 */

public class KeyEvent extends JFrame implements KeyListener
{
   private JPanel panel;
   private JLabel tecla,codigo;
   private JTextField txttecla,txtcodigo;
   private int        c1 =0,c2=0,c3=0;
   private Random     rnd;
   
   public KeyEvent()
   {
      this.setTitle("Eventos del Teclado");
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setSize(400,200);
      crear();
      this.setVisible(true);
   }
   
   public void crear()
   {
      panel=new JPanel();
      rnd=new Random();
      tecla=new JLabel("Tecla Pulsada: ");
      codigo=new JLabel("Codigo de la Tecla Pulsada: ");
      txttecla=new JTextField(15);
      txtcodigo=new JTextField(15);
      txttecla.setEditable(false);
      txttecla.addKeyListener(this);
      txtcodigo.addKeyListener(this);
      txtcodigo.setEditable(false);
      panel.add(tecla);
      panel.add(txttecla);
      panel.add(codigo);
      panel.add(txtcodigo);
      this.add(panel);
   }
   
   public void keyPressed(java.awt.event.KeyEvent pE)
   {
      txttecla.setText(java.awt.event.KeyEvent.getKeyText(pE.getKeyCode()));
      c1=rnd.nextInt(255);
      c2=rnd.nextInt(255);
      c3=rnd.nextInt(255);
      txtcodigo.setText(pE.getKeyCode()+"");
      panel.setBackground(new Color(c1,c2,c3));
   }
   
   public void keyReleased(java.awt.event.KeyEvent pE)
   {
      try
      {
         Thread.sleep(200);
      }
      catch(InterruptedException e)
      {
         e.printStackTrace();
      }
      txttecla.setText("Usted pulso y solto la tecla: "+java.awt.event.KeyEvent.getKeyText(pE.getKeyCode()));
      c1=rnd.nextInt(255);
      c2=rnd.nextInt(255);
      c3=rnd.nextInt(255);
      tecla.setForeground(new Color(c1,c2,c3));
      codigo.setForeground(new Color(c1,c2,c3));
   }
   
   public void keyTyped(java.awt.event.KeyEvent pE)
   {
   }
   
   public static void main(String[] args)
   {
      new Apariencia(Apariencia.SYNTHETICA_BLUE_LIGHT);
      new KeyEvent();
   }
}
